﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using COMOSOFT.Class;
using System.Data.SqlClient;
using System.Net.Mail;
using COMOSOFT.JobManagement;


namespace COMOSOFT.JobManagement.Froms
{
    public partial class TabStatus : Form
    {
        #region [Init Form]
        ClassShare cls = new ClassShare("TabStatus");
        int JID;
        string title, date, body;
        DateTime enddate;
        string assigner;
        string staff;
        List<string> ListEmail = new List<string>();        
        public TabStatus(int _JID,string _title,string _date, string _body,DateTime _endate)
        {
            InitializeComponent();
            this.JID = _JID;
            this.title = _title;
            this.date = _date;
            this.body = _body;
            this.enddate = _endate;
        }
        #endregion

        #region [Cancel]
        private void bnt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion 

        #region [Assign to Staff]
        private void bnt_ok_Click(object sender, EventArgs e)
        {
            try
            {
                bool flag = true;
                cls.UpdateData("sp_JobSubmiterUpdate", new string[] { "@JID", "@EID", "@Status", "@Comment", "@flag" }, new object[] { JID, ClassShare.Usid, "Next assign to", txt_Comment.Text, 1 });
                int[] EIDs = gridView_ChoseEmployee.GetSelectedRows();
                foreach (int item in EIDs)
                {
                    if (gridView_ChoseEmployee.GetDataRow(item)["DealDate"].ToString() == "" || Convert.ToDateTime(gridView_ChoseEmployee.GetDataRow(item)["DealDate"]) > enddate)
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == true)
                {
                    foreach (int item in EIDs)
                    {
                        staff += gridView_ChoseEmployee.GetDataRow(item)["FullName"].ToString() + " ;";
                        cls.UpdateData("sp_JobSubmiterInsert", new string[] { "@JID", "@EID", "@Assigner", "@ReceDate", "@DealDate" }, new object[] { JID, Convert.ToInt32(gridView_ChoseEmployee.GetDataRow(item)["EID"]), ClassShare.Usid, DateTime.Now, Convert.ToDateTime(gridView_ChoseEmployee.GetDataRow(item)["DealDate"]) });
                    }
                    if (MessageBox.Show("Do you want to send email to Assigner?", "Send Email...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        SendMail();
                    }
                    this.Close();
                }
                else
                    MessageBox.Show("Time is null or greater job time");
            }
            catch
            {
                MessageBox.Show("Not thing update!", "Update fail...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }           
        }
        #endregion

        #region [Form Load]
        private void TabStatus_Load(object sender, EventArgs e)
        {
            this.sp_JobChoseStaffTableAdapter.Fill(dataSet_JobManagement.sp_JobChoseStaff, JID);
        }
        #endregion

        #region [Send Email]
        public void SendMail()
        {
            try
            {
                ListEmail.Clear();
                MailMessage message = new MailMessage();
                SmtpClient Client = new SmtpClient();
                Client.Host = "smtp.gmail.com";
                Client.Port = 587;
                Client.Credentials = new System.Net.NetworkCredential("comosoft.como@gmail.com", "@Comosoft324");
                Client.EnableSsl = true;
                Client.DeliveryMethod = SmtpDeliveryMethod.Network;
                message.From = new MailAddress(ClassShare.Email);
                message.Sender = new MailAddress(ClassShare.Email);
                sp_JobGetEmailTableAdapter.Fill(dataSet_JobManagement.sp_JobGetEmail, JID);
                DataTable dtb = dataSet_JobManagement.sp_JobGetEmail;
                if (dtb.Rows.Count > 0)
                {
                    int numrow = dtb.Rows.Count - 1;
                    assigner = dtb.Rows[numrow]["Assigner"].ToString();
                    ListEmail.Add(dtb.Rows[numrow]["Email"].ToString());
                    for (int i = numrow; i >= 0; i--)
                    {
                        if (assigner == dtb.Rows[i]["EID"].ToString())
                        {
                            if (dtb.Rows[i]["Email"] != null && dtb.Rows[i]["Email"].ToString() != "")
                                ListEmail.Add(dtb.Rows[i]["Email"].ToString());
                            assigner = dtb.Rows[i]["Assigner"].ToString();
                        }
                    }
                }
                foreach (string item in ListEmail)
                {
                    message.To.Add(new MailAddress(item));
                }

                message.Subject = "Next Assign to job";
                message.Body = ClassShare.Usname+ " assigned to job to " + staff +
                               " </br> Job Information : " +
                               " </br> Job Name: " + title +
                               " </br> Job Content: " + body+
                               " </br> Create Date: " + date;
                message.IsBodyHtml = true;
                Client.Send(message);                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Close();
            }
        }
        #endregion
    }
}
